class Solution {
public:
    int numSquares(int n)
    {
        const int INF = 0x3f3f3f3f;
        int m = sqrt(n);
        vector<int> dp(n + 1);
        for (int j = 1; j <= n; j++) dp[j] = INF;
        for (int i = 1; i <= m; i++)
        {
            for (int j = i * i; j <= n; j++)
            {
                dp[j] = min(dp[j], dp[j - i * i] + 1);
            }
        }
        return dp[n];
    }
};
class Solution {
public:
    int change(int amount, vector<int>& coins)
    {
        int m = coins.size();
        vector<double> dp(amount + 1);
        dp[0] = 1;
        for (int j = 1; j <= amount; j++) dp[j] = 0;
        for (int i = 1; i <= m; i++)
        {
            for (int j = coins[i - 1]; j <= amount; j++)
            {
                if (dp[j - coins[i - 1]] != -1) dp[j] += dp[j - coins[i - 1]];
            }
        }
        return dp[amount];
    }
};
class Solution {
public:
    int coinChange(vector<int>& coins, int amount)
    {
        const int INF = 0x3f3f3f3f;
        int m = coins.size();
        vector<int> dp(amount + 1);
        for (int j = 1; j <= amount; j++) dp[j] = INF;

        for (int i = 1; i <= m; i++)
        {
            for (int j = coins[i - 1]; j <= amount; j++)
            {
                dp[j] = min(dp[j], dp[j - coins[i - 1]] + 1);
            }
        }
        return dp[amount] == INF ? -1 : dp[amount];
    }
};